//
//  HJQRCode.m
//  HJQRCode
//
//  Created by HJ on 2016/10/21.
//  Copyright © 2016年 HJ. All rights reserved.
//

#import "HJQRCode.h"

@implementation HJQRCode

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/


+(HJQRCode *)creatCodeWithDataString:(NSString *)dataString superView:(UIView *)superView withSize:(CGFloat) size
{
    HJQRCode * code = [[HJQRCode alloc] init];
    code.frame = CGRectMake(0, 0, superView.frame.size.width, superView.frame.size.height);
    UIImage * image = [code codeWithDataString:dataString withSize:size];
    code.layer.contents = (__bridge id)image.CGImage;
    [superView addSubview:code];
    return code;

}


/**生成不带图片的二维码
 * @param dataString 添加数据(正则表达式/账号和密码)
 */
-(UIImage *)codeWithDataString:(NSString *)dataString withSize:(CGFloat) size
{
    // 1.创建过滤器
    CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
    // 2.恢复默认
    [filter setDefaults];
    // 3.data转换
    NSData *data = [dataString dataUsingEncoding:NSUTF8StringEncoding];
    [filter setValue:data forKeyPath:@"inputMessage"];
    // 4.获取输出的二维码
    CIImage * outputImage = [filter outputImage];
    
    return [self createNonInterpolatedUIImageFormCIImage:outputImage withSize:size];
}

/**根据CIImage生成指定大小的UIImage
 * @param image CIImage
 * @param size 图片宽度
 */
- (UIImage *)createNonInterpolatedUIImageFormCIImage:(CIImage *)image withSize:(CGFloat) size
{
    CGRect extent = CGRectIntegral(image.extent);
    CGFloat scale = MIN(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent));
    // 1.创建bitmap;
    size_t width = CGRectGetWidth(extent) * scale;
    size_t height = CGRectGetHeight(extent) * scale;
    CGColorSpaceRef cs = CGColorSpaceCreateDeviceGray();
    CGContextRef bitmapRef = CGBitmapContextCreate(nil, width, height, 8, 0, cs, (CGBitmapInfo)kCGImageAlphaNone);
    CIContext *context = [CIContext contextWithOptions:nil];
    CGImageRef bitmapImage = [context createCGImage:image fromRect:extent];
    CGContextSetInterpolationQuality(bitmapRef, kCGInterpolationNone);
    CGContextScaleCTM(bitmapRef, scale, scale);
    CGContextDrawImage(bitmapRef, extent, bitmapImage);
    // 2.保存bitmap到图片
    CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef);
    CGContextRelease(bitmapRef);
    CGImageRelease(bitmapImage);
    return [UIImage imageWithCGImage:scaledImage];
}
/**对二维码进行颜色填充
 */

/**
 *  生成带logo的二维码
 *  二维码和logo都是正方形的
 *  @param urlString     二维码中的链接
 *  @param superView     放入的视图
 *  @param logoImage     二维码中的logo
 *  @param logoImageSize logo的大小
 *  @param cornerRadius  logo的圆角值大小
 *
 *  @return 生成的二维码
 */
+ (HJQRCode *)creatImageCodeWithURLString:(NSString *)urlString superView:(UIView *)superView logoImage:(UIImage *)logoImage logoImageSize:(CGSize)logoImageSize logoImageWithCornerRadius:(CGFloat)cornerRadius
{
    // 先移除子视图
    HJQRCode *oldQRCodeView = [superView viewWithTag:123];
    [oldQRCodeView removeFromSuperview];
    
    HJQRCode *QRCodeView = [[HJQRCode alloc] init];
    QRCodeView.tag = 123;
    QRCodeView.frame = CGRectMake(0, 0, superView.frame.size.width, superView.frame.size.height);
    
    //生成二维码
    UIImage  * qrImage = [QRCodeView codeWithDataString:urlString withSize:superView.frame.size.width];
    
    if (logoImage != nil) {
        if (cornerRadius < 0) {
            cornerRadius = 0;
            NSLog(@"cornerRadius 不能小于0");
        }
        qrImage = [QRCodeView addImageToSuperImage:qrImage withSubImage:[QRCodeView imageWithCornerRadius:cornerRadius image:logoImage] andSubImagePosition:CGRectMake((superView.frame.size.width - logoImageSize.width)/2, (superView.frame.size.height - logoImageSize.height)/2, logoImageSize.width, logoImageSize.height)]; // 增加logo
    }
    QRCodeView.layer.contents = (__bridge id)qrImage.CGImage;
    [superView addSubview:QRCodeView];
    return QRCodeView;
}
/**
 *  图片设置圆角
 *
 *  @param cornerRadius 圆角值
 *  @param image     图片
 *
 *  @return 圆角图片
 */
- (UIImage *)imageWithCornerRadius:(CGFloat)cornerRadius image:(UIImage *)image
{
    CGRect frame = CGRectMake(0, 0, image.size.width, image.size.height);
    UIGraphicsBeginImageContextWithOptions(image.size, NO, 1.0);
    [[UIBezierPath bezierPathWithRoundedRect:frame
                                cornerRadius:cornerRadius] addClip];
    // 画图
    [image drawInRect:frame];
    // 获取新的图片
    UIImage *im = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return im;
}

/**
 *  图片增加水印
 *
 *  @param superImage 需要增加水印的图片
 *  @param subImage   水印图片
 *  @param posRect    水印的位置 和 水印的大小
 *
 *  @return 加水印后的新图片
 */
- (UIImage *)addImageToSuperImage:(UIImage *)superImage withSubImage:(UIImage *)subImage andSubImagePosition:(CGRect)posRect{
    
    UIGraphicsBeginImageContext(superImage.size);
    [superImage drawInRect:CGRectMake(0, 0, superImage.size.width, superImage.size.height)];
    //四个参数为水印图片的位置
    [subImage drawInRect:posRect];
    UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return resultingImage;
}



@end
